/*
 * $Id: BaseXmlParser.java,v 1.3 2009/05/29 16:15:33 afaichun Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the
 * General Electric Company (GE). You shall not disclose this software and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.xml.parser;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Purpose: 
 * <ul>
 * <li>Parent class for all parsers</li>
 * <li>Implements SAX Handler</li>
 * <li>Response for conversion of parameters that are supposed
 * to be present in all xmls</li>
 * </ul>
 * @author 212042946
 * @copyright (C) 2009 The General Electric Company
 */
public abstract class BaseXmlParser extends DefaultHandler implements IXmlParser{

	/**
	 * Default Handler
	 */
	protected DefaultHandler objDHandler = null;
	/**
	 * Sax Parser Object
	 */
	protected SAXParser objSAXParser = null;
	/**
	 * Current parsed element
	 */
	protected String strParseElement = null;
	/**
	 * Value for Current parsed element
	 */
	protected String strElementValue = null;
	/**
	 * Byte Input Stream
	 */
	protected ByteArrayInputStream objInputStream = null;

	private static final String JAXP_SCHEMA_SOURCE = 
		"http://java.sun.com/xml/jaxp/properties/schemaSource";
	
	private static final String JAXP_SCHEMA_LANGUAGE = 
		"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
	
	/**
	 * Creates the Handler for parsing
	 * @throws SAXException 
	 * @throws ParserConfigurationException 
	 */
	public BaseXmlParser(String schemaFileStr) throws ParserConfigurationException, SAXException
	{
		//Creates the Handlers
		this.createHandler(schemaFileStr);
	}
	
	/**
	 * Creates the Handler for parsing
	 * @throws SAXException 
	 * @throws ParserConfigurationException 
	 */
	private void createHandler(String schemaFileStr) throws ParserConfigurationException, SAXException 
	{
			// Obtain a new instance of a SAXParserFactory.
			SAXParserFactory objSAXFactory = SAXParserFactory.newInstance();

			// Specifies that the parser produced by this code will provide support for XML namespaces.
   			objSAXFactory.setNamespaceAware(false);

			// Specifies that the parser produced by this code will validate documents as they are parsed.
   			objSAXFactory.setValidating(true);

			// Creates a new instance of a SAXParser using the currently configured factory parameters.
			objSAXParser = objSAXFactory.newSAXParser();
			
			objSAXParser.setProperty(JAXP_SCHEMA_LANGUAGE, "http://www.w3.org/2001/XMLSchema");
			objSAXParser.setProperty(JAXP_SCHEMA_SOURCE, new File(schemaFileStr));
	}

	/**
	 * Call parse on the SAXParser to parse the xml
	 * @param strXML
	 * @throws IOException 
	 * @throws SAXException 
	 * @throws Exception
	 */
	public void parseResponse(String strXML) throws SAXException, IOException
	{
		objInputStream = new ByteArrayInputStream(strXML.getBytes());
		objDHandler = this;
		objSAXParser.parse(objInputStream, objDHandler);
	}
	
	/**
	 * Call parse on the SAXParser to parse the xml
	 * @param strXML
	 * @throws IOException 
	 * @throws SAXException 
	 * @throws Exception
	 */
	public void parseResponse(File xmlFile) throws SAXException, IOException
	{
		objDHandler = this;
		objSAXParser.parse(xmlFile, objDHandler);
	}
	
	
  	/**
  	 * Selecting the parseElement
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String strNamespaceURI, String strLocalName, String strQName, Attributes attrs)
	throws SAXException
	{
		//Generic content common for all XMLs can be placed here
		strElementValue = null;
	}

  	/**
  	 * Storing the values
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void endElement(String strNamespaceURI, String strLocalName, String srQName) throws SAXException
    {
		//Generic content common for all XMLs can be placed here
    }

	/**
	 * Extracting the value
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	public void characters(char[] arrCh, int iStart, int iLength) throws SAXException
	{
		//Generic content common for all XMLs can be placed here
	}
}
